<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" class="mt-20" border>
      <el-table-column align="center" label="大区ID" width="80px" prop="id"></el-table-column>
      <el-table-column align="center" label="大区名称" width="120px" prop="name"></el-table-column>
      <el-table-column align="center" label="创角限制" width="180px" prop="limit"></el-table-column>
      <el-table-column align="center" label="自动开服" width="90px">
        <template slot-scope="{row}">
          <el-tag :type="row.is_auto_open | statusTagFilter">{{ row.is_auto_open | statusFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态修改" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.new_status | serverStatusFilter }}</span> |
          <span>{{ row.old_status | serverStatusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="推荐修改" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.new_tag | serverTagFilter }}</span> |
          <span>{{ row.old_tag | serverTagFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="推荐模式" width="110px">
        <template slot-scope="{row}">
          <el-tag v-if="row.recommend_mode === 1" type="success">单</el-tag>
          <el-tag v-else type="warning">多</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="避免时间" width="180px" prop="avoid_time"></el-table-column>
      <el-table-column align="center" label="开服条件" width="120px" prop="condition_str"></el-table-column>
      <el-table-column align="center" label="定时开服" prop="timing"></el-table-column>
      <el-table-column align="center" label="开服通知">
        <template slot-scope="{row}">
          <el-tag v-if="row.server_notify === '1'" type="success">开启</el-tag>
          <el-tag v-else type="warning">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120px">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(permissionRoles.server_region_edit)" type="text" @click="handleEdit(scope)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="80px" label-position="left">
        <el-form-item label="大区ID" prop="id">
          <el-input v-model="dataForm.id" :disabled="idDisabled" class="defaut-input" placeholder="填写大区ID" />
        </el-form-item>
        <el-form-item label="大区名称" prop="name">
          <el-input ref="name" v-model="dataForm.name" :disabled="idDisabled" class="defaut-input" placeholder="填写大区名称" />
        </el-form-item>
        <el-form-item label="创角限制" prop="limit">
          <el-input v-model="dataForm.open_days" class="defaut-input" style="width: 100px;" placeholder="天数" />
          <el-input v-model="dataForm.create_roles" class="defaut-input" style="width: 100px;" placeholder="角色数" />
        </el-form-item>
        <el-form-item label="自动开服">
          <el-radio-group v-model="dataForm.is_auto_open">
            <el-radio :label="0">关闭</el-radio>
            <el-radio :label="1">开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推荐模式" prop="recommend_mode">
          <el-select v-model="dataForm.recommend_mode" style="width: 120px;" placeholder="请选择">
            <el-option label="单" value="1"></el-option>
            <el-option label="多" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态修改" prop="modify_status">
          <el-select v-model="dataForm.new_status" style="width: 120px;" placeholder="请选择">
            <el-option label="无" value="0"></el-option>
            <el-option label="流畅" value="1"></el-option>
            <el-option label="拥挤" value="2"></el-option>
            <el-option label="火爆" value="3"></el-option>
            <el-option label="维护" value="4"></el-option>
          </el-select>
          <el-select v-model="dataForm.old_status" style="width: 120px;" placeholder="请选择">
            <el-option label="无" value="0"></el-option>
            <el-option label="流畅" value="1"></el-option>
            <el-option label="拥挤" value="2"></el-option>
            <el-option label="火爆" value="3"></el-option>
            <el-option label="维护" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标记修改" prop="modify_status">
          <el-select v-model="dataForm.new_tag" style="width: 120px;" placeholder="请选择">
            <el-option label="无" value="0"></el-option>
            <el-option label="新服" value="1"></el-option>
            <el-option label="推荐" value="2"></el-option>
          </el-select>
          <el-select v-model="dataForm.old_tag" style="width: 120px;" placeholder="请选择">
            <el-option label="无" value="0"></el-option>
            <el-option label="新服" value="1"></el-option>
            <el-option label="推荐" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="避免时间">
          <el-form-item>
            <el-time-select v-model="dataForm.avoid_start_time" :picker-options="{ start: '00:00', step: '00:5', end: '23:55' }" placeholder="选择时间" style="width: 27%"></el-time-select>
            <el-time-select v-model="dataForm.avoid_end_time" :picker-options="{ start: '00:00', step: '00:5', end: '23:55' }" placeholder="选择时间" style="width: 27%"></el-time-select>
          </el-form-item>
        </el-form-item>
        <el-form-item label="开服条件" prop="condition">
          <el-select v-model="dataForm.condition_type" style="width: 100px;" placeholder="请选择">
            <el-option label="或" value="1"></el-option>
            <el-option label="且" value="2"></el-option>
          </el-select>
          <el-input v-model="dataForm.role_num" style="width: 100px;" class="defaut-input" placeholder="创建人数" />
          <el-input v-model="dataForm.recharge_num" style="width: 100px;" class="defaut-input" placeholder="充值人数" />
        </el-form-item>
        <el-form-item label="开服通知" prop="server_notify">
          <el-switch v-model="dataForm.server_notify" active-value="1" inactive-value="0" active-text="开启" inactive-text="关闭"></el-switch>
        </el-form-item>
        <el-form-item label="定时开服">
          <el-row>
            <el-col :md="3"><el-button type="primary" class="add" size="mini" icon="el-icon-plus" @click="timingAdd">新增</el-button></el-col>
          </el-row>
          <el-row v-for="(item, index) in dataForm.timings" :key="index" class="mt-5" :gutter="12">
            <el-col :md="6">
              <el-time-select v-model="item.time" :picker-options="{ start: '00:00', step: '00:5', end: '23:55' }" placeholder="选择时间"></el-time-select>
            </el-col>
            <el-col :md="4"><el-input v-model="item.days" placeholder="间隔天数" /></el-col>
            <el-col :md="4"><el-button type="danger" size="mini" icon="el-icon-delete" @click.prevent="remove(item, index)" /></el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { deepClone } from '@/utils'
import checkPermission from '@/utils/permission'
import { getServerRegionList, handleServerStrategy } from '@/api/ops'

const defaultItem = {
  id: '',
  name: '',
  is_auto_open: 0,
  title: '',
  remark: '',
  open_days: '',
  create_roles: '',
  condition_type: '',
  recommend_mode: '1',
  avoid_start_time: '',
  avoid_end_time: '',
  new_status: '1',
  old_status: '1',
  new_tag: '0',
  old_tag: '0',
  role_num: '',
  recharge_num: '',
  server_notify: '0',
  timings: [{
    time: '',
    days: ''
  }]
}

export default {
  name: 'ServerRegion',
  filters: {
    statusFilter(is_auto_open) {
      const statusMap = {
        0: '否',
        1: '是'
      }
      return statusMap[is_auto_open]
    },
    serverStatusFilter(status) {
      const serverStatusMap = {
        0: '无状态',
        1: '流畅',
        2: '拥挤',
        3: '火爆',
        4: '维护'
      }
      return serverStatusMap[status]
    },
    serverTagFilter(status) {
      const serverTagMap = {
        0: '无',
        1: '新服',
        2: '推荐'
      }
      return serverTagMap[status]
    },
    statusTagFilter(status) {
      const statusTagMap = {
        0: 'warning',
        1: 'success'
      }
      return statusTagMap[status]
    }
  },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      rules: {
        id: [{ required: true, message: 'ID必填', trigger: 'blur' }],
        name: [{ required: true, message: '名称必填', trigger: 'blur' }]
      },
      list: [],
      dialogVisible: false,
      idDisabled: false,
      srid: '',
      srname: '',
      dialogType: 'new',
      textMap: {
        edit: '编辑',
        new: '新增'
      }
    }
  },
  computed: {
  },
  created() {
    this.getServerRegionList()
  },
  methods: {
    checkPermission,
    async getServerRegionList() {
      this.listLoading = true
      const res = await getServerRegionList()
      this.listLoading = false
      this.list = res.data
      res.data.forEach(element => {
        if (element.error_battle_intercept === 1) this.battleSwitch = '1'
      })
    },

    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.idDisabled = true
      const row = scope.row
      row.recommend_mode = row.recommend_mode + ''
      row.condition_type = row.condition_type + ''

      if (row.timing === null) row.timings = ''
      if (Array.isArray(row.timing) === false) {
        if (row.timing === '') {
          row.timings = [{ time: '', days: '' }]
        } else {
          const ts = row.timing.split(',')
          const a = []
          for (let i = 0; i < ts.length; i++) {
            const o = { time: '', days: '' }
            const str = ts[i].replace(/\[|\]/g, '')
            const e = str.split('|')
            o.time = e[0]
            let days = ''
            if (e[1]) days = parseInt(e[1])
            o.days = days
            a[i] = o
          }
          row.timings = a
        }
      }
      this.dataForm = deepClone(row)

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleServerStrategy(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.getServerRegionList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            }
          })
        }
      })
    },
    timingAdd(row) {
      if (this.dataForm.timings.length === 6) {
        return
      }
      this.dataForm.timings.push({
        time: '',
        days: ''
      })
    },
    remove(row, index) {
      if (index !== -1) {
        this.dataForm.timings.splice(index, 1)
      }
    },
    c2Add(row) {
      if (this.chatDataForm.channel2.levels.length === 5) {
        return
      }
      this.chatDataForm.channel2.levels.push({
        day: '',
        level: ''
      })
    },
    removec2(row, index) {
      if (index !== -1) {
        this.chatDataForm.channel2.levels.splice(index, 1)
      }
    },
    c3Add(row) {
      if (this.chatDataForm.channel3.levels.length === 5) {
        return
      }
      this.chatDataForm.channel3.levels.push({
        day: '',
        level: ''
      })
    },
    removec3(row, index) {
      if (index !== -1) {
        this.chatDataForm.channel3.levels.splice(index, 1)
      }
    },
    c4Add(row) {
      if (this.chatDataForm.channel4.levels.length === 5) {
        return
      }
      this.chatDataForm.channel4.levels.push({
        day: '',
        level: ''
      })
    },
    removec4(row, index) {
      if (index !== -1) {
        this.chatDataForm.channel4.levels.splice(index, 1)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table {
  font-size: 14px;
}
.el-table td, .el-table th {
  padding: 10px 0;
}
.el-table th>.cell {
  padding-left: 10px;
  padding-right: 10px;
}
</style>
